package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.util.UUIDUtil;
import com.macro.mall.mapper.admin.KeysTypeMapper;
import com.macro.mall.model.admin.KeysType;
import com.macro.mall.query.KeyTypeQuery;
import com.macro.mall.service.KeysTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class KeysTypeServiceImpl implements KeysTypeService {


    @Autowired
    private KeysTypeMapper keysTypeMapper;


    /***
     * 查询 keys类型列表
     * @param keyTypeQuery
     * @return
     */
    @Override
    public CommonResult getClassList(KeyTypeQuery keyTypeQuery) {
        Map<String, Object> result = new HashMap<>();
        PageHelper.startPage(keyTypeQuery.getPage(), keyTypeQuery.getPageSize());
        List<KeysType> keysTypes = keysTypeMapper.selectList(keyTypeQuery);
        PageInfo<KeysType> basicKeyPageInfo = new PageInfo<>(keysTypes);
        result.put("list", basicKeyPageInfo.getList());
        result.put("total", basicKeyPageInfo.getTotal());
        return CommonResult.success(result);
    }


    /***
     * 编辑 keys类型列表
     * @param keysType
     * @return
     */
    @Override
    public CommonResult edit(KeysType keysType) {
        if (StringUtils.isEmpty(keysType.getId())) {
            //新增
            keysType.setId(UUIDUtil.getUUid());
            keysType.setCreationTime(new Date());
            keysType.setUpdateTime(new Date());
            int row = keysTypeMapper.insertSelective(keysType);
            return result(row, "新增keys类型失败");
        }
        keysType.setUpdateTime(new Date());
        int row = keysTypeMapper.updateByPrimaryKeySelective(keysType);
        return result(row, "修改keys类型失败");
    }


    /***
     * 删除 keys类型列表
     * @param id
     * @return
     */
    @Override
    public CommonResult removeClass(String id) {
        int row = keysTypeMapper.deleteByPrimaryKey(id);
        return result(row, "删除 keys类型失败");
    }


    /****
     * id查询 keys类型
     * @param id
     * @return
     */
    @Override
    public CommonResult findClass(String id) {
        KeysType keysType = keysTypeMapper.selectByPrimaryKey(id);
        return CommonResult.success(keysType);
    }


    /***
     * 判断修改数据库返回值 返回
     * @param row
     * @param msg
     * @return
     */
    private CommonResult result(int row, String msg) {
        if (row > 0) {
            return CommonResult.success(null);
        }
        return CommonResult.failed(msg);
    }
}
